package com.example.ythhome.login.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ythhome.busi.entity.BusiCode;
import com.example.ythhome.busi.service.BusiCodeService;
import com.example.ythhome.login.entity.*;
import com.example.ythhome.login.service.RoleMenuService;
import com.example.ythhome.login.service.RoleService;
import com.example.ythhome.login.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("api/role")
public class RoleController {

    @Autowired
    RoleService roleService;


    @Autowired
    BusiCodeService busiCodeService;

    @PostMapping("/query")
    @ResponseBody
    public ResponseData<IPage> query(@RequestBody RequestData<Role> requestData) {
        Role role = requestData.getData();
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("role_name", role.getRoleName())
                .like("role_sign", role.getRoleSign())
                .orderByAsc("role_no");
        if(!"".equals(role.getState())&&role.getState()!=null){
            queryWrapper.eq("state", role.getState());
        }
        Page<Role> page = new Page<>();
        page.setCurrent(requestData.getPagination().getCurrentPage());
        page.setSize(requestData.getPagination().getPageSize());
        return new ResponseData<IPage>(ResponseData.StatusCodeEnum_SUCCESS, roleService.page(page,queryWrapper));
    }

    @PostMapping("/queryAll")
    @ResponseBody
    public ResponseData<List> list() {
        return new ResponseData<List>(ResponseData.StatusCodeEnum_SUCCESS, roleService.list());
    }

    @PostMapping("/add")
    @ResponseBody
    public ResponseData<Boolean> add(@RequestBody Role role) {
        role.setState("1");
        role.setRoleNo(busiCodeService.getBusiCode("ROLE_NO"));
        return new ResponseData<Boolean>(ResponseData.StatusCodeEnum_SUCCESS, roleService.save(role));
    }

    @PostMapping("/edit")
    @ResponseBody
    public ResponseData<Boolean> edit(@RequestBody Role role) {
        UpdateWrapper<Role> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("role_id",role.getRoleId());
        return new ResponseData<Boolean>(ResponseData.StatusCodeEnum_SUCCESS, roleService.update(role,updateWrapper));
    }

    @PostMapping("/delete")
    @ResponseBody
    public ResponseData<Boolean> delete(@RequestBody Integer id) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", id);
        return new ResponseData<Boolean>(ResponseData.StatusCodeEnum_SUCCESS, roleService.remove(queryWrapper));
    }


}
